package cn.bdqn.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import cn.bdqn.bean.User;
import cn.bdqn.bean.UserType;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.UserDao;
import cn.bdqn.dao.impl.UserDaoImpl;
import cn.bdqn.service.UserService;

public class UserServiceImpl implements UserService{
	UserDao userDao = null;
	public UserServiceImpl(){
		userDao = new UserDaoImpl();
	}
	
	/**
	 * 新增用户
	 * 
	 */
	@Override
	public boolean addUser(User user) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = userDao.addUser(con, user);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("add success");
			return true;
		}else{
			System.out.println("add failure");
			return false;
		}

	}
	/**
	 * 修改用户
	 */
	@Override
	public boolean updateUser(User user) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = userDao.updateUser(con, user);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("update success");
			return true;
		}else{
			System.out.println("update failure");
			return false;
		}
		
	}
	/**
	 * 删除用户
	 */
	@Override
	public boolean deleteUser(User user) {
		int row = 0;
		//开启连接
		Connection con = BaseDao.getConnetion();
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			row = userDao.deleteUser(con, user);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		if(row > 0){
			System.out.println("delete success");
			return true;
		}else{
			System.out.println("delete failure");
			return false;
		}
		
	}
	
	

	@Override
	public int getCount(User user){
		//开启连接
		Connection con = BaseDao.getConnetion();
		int count = 0;
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			count = userDao.getCount(con, user);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return count;
	}

	@Override
	public List<User> getList(User user, int pageIndex,
			int pageSize) {
		//开启连接
		Connection con = BaseDao.getConnetion();
		List<User> list = null;
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			list = userDao.getList(con, user, pageIndex, pageSize);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return list;
	}

	@Override
	public User getUserById(int id) {
		//开启连接
		Connection con = BaseDao.getConnetion();
		User user = null;
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			user = userDao.getUserById(con, id);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return user;
	}

	@Override
	public List<UserType> getUserTypeList() {
		//开启连接
		Connection con = BaseDao.getConnetion();
		List<UserType> list = null;
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			list = userDao.getUserTypeList(con);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return list;
	}

	/**
	 * 登录方法，返回完整的user信息
	 */
	@Override
	public User login(User user) {
		//开启连接
		Connection con = BaseDao.getConnetion();
		User u = null;
		try {
			//用连接开启jdbc连接数据库的 事务
			con.setAutoCommit(false);
			u = userDao.login(con, user);
			con.commit();
		} catch (SQLException e) {
			//捕捉异常并回滚事务
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			//关闭连接
			BaseDao.closeConnection(con, null, null);
		}
		return u;
	}

}
